//
//  MyBillVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/16.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "MyBillVC.h"
#import "MyBillCell3.h"
#import "MyBillCell.h"
#import "MyBillForDayVC.h"
#import "ClubBillCell.h"
@interface MyBillVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation MyBillVC
{
    UITableView *_tableViewWithBill;
    NSDictionary *_responseDic;
    NSArray *_responseArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = superBackgroundColor;
    self.title = @"我的账单";
    [self requestForTouristBillList];
    [self initTableViewWithBill];
}
- (void)requestForTouristBillList{
    //参数
    NSDictionary *parameters = @{
                                 @"userID":[NSString readToNSDefaultsWithKey:kId],
//                                 @"status": @"2",
                                 };
    [SVProgressHUD showWithStatus:@"请求中"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/userBillList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //请求成功
            [SVProgressHUD showSuccessWithStatus:@"请求成功"];
            [_tableViewWithBill reloadData];
            [SVProgressHUD dismiss];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
- (void)initTableViewWithBill{
    _tableViewWithBill = [[UITableView alloc]initWithFrame:self.view.frame];
    //    _tableView.backgroundColor = [UIColor grayColor];
    _tableViewWithBill.delegate = self;
    _tableViewWithBill.dataSource = self;
    _tableViewWithBill.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableViewWithBill];
}
#pragma - mark UITableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 96;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ClubBillCell *cell = [[NSBundle mainBundle]loadNibNamed:@"ClubBillCell" owner:nil options:nil][0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = superBackgroundColor;
    //cell的bgView的阴影效果
    cell.bgView.layer.shadowColor = [UIColor grayColor].CGColor;
    cell.bgView.layer.shadowOffset = CGSizeMake(2,2);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    cell.bgView.layer.shadowOpacity = 0.5;
    cell.bgView.layer.shadowRadius = 2;
    
    cell.titleLb.text = _responseArr[indexPath.row][@"activityName"];
    cell.nameLb.text = _responseArr[indexPath.row][@"leaderName"];
    NSNumber *num = _responseArr[indexPath.row][@"activityTotalMoney"];
    cell.moneyLb.text = num.stringValue;
    NSString *timeStr = _responseDic[@"list"][indexPath.row][@"activityBeginTime"];
    timeStr = [timeStr substringToIndex:10];
    cell.timeLb.text = timeStr;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyBillForDayVC *vc = [[MyBillForDayVC alloc]init];
    vc.activityName = _responseArr[indexPath.row][@"activityName"];
    vc.totalMoney = _responseArr[indexPath.row][@"activityTotalMoney"];
    vc.dayArr = _responseArr[indexPath.row][@"billList"];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
